package com.teckteam.ptvsystem;

import java.io.FileNotFoundException;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import com.teckteam.ptvsystem.model.facade.PTVModel;
import com.teckteam.ptvsystem.model.facade.PTVSystem;
import com.teckteam.ptvsystem.view.LoadingDialog;
import com.teckteam.ptvsystem.view.PTVUserInterface;

/**
 * Main entrypoint class.  Everything starts from here.
 */
public class PTVLauncher
{
   private static PTVModel model;
   
   /**
    * Main application entrypoint
    * 
    * @param args
    */
   public static void main(String[] args)
   {
      try
      {
         //Use look and feel of OS
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         
         //Load singleton instance of model
         model = PTVSystem.getInstance();
         
         // Attempt to load existing timetable LinkedHashMap objects before proceeding.
         int fileLoadCount = model.importTimetableFiles();
         
         // If no files loaded, warn user timetable system is empty
         if (fileLoadCount == 0)
         {
            JOptionPane
                  .showOptionDialog(
                        null,
                        "Please be advised that there are currently no timetables present in the system.\n"
                              + "After you press OK, please run Update before you can proceed.",
                        "Empty database", JOptionPane.DEFAULT_OPTION,
                        JOptionPane.WARNING_MESSAGE, null, null, null);
         }
         
         
      } catch (FileNotFoundException e)
      {
         JOptionPane.showOptionDialog(
               null, "Error loading GPS injection data.\nProgram will now terminate. (See README.TXT for details)",
               "GPS injection failed", JOptionPane.DEFAULT_OPTION,
               JOptionPane.ERROR_MESSAGE, null, null, null);
         System.exit(1);
      } catch (Exception e)
      {
         //Do not care
         System.err.println(e.getMessage());
      }
      
      LoadingDialog.getInstance().setVisible(false);
      PTVUserInterface.getInstance().refreshView();
   }
}
